## ************************************************************************** ## 
## Project: Gov 2020 Project
## Author: Amelia Malpas, Leah Stein & David Struhs
## Date Created: September 27, 2024
## Date Edited: November 27, 2024
## Task: Create CHIPS Table and Plots
## ************************************************************************** ## 

## -------------------------------------------------------------------------- ## 
## Input:"countypres_2000-2020.csv"
## Output: CHIPS Funding Table, DID County Plot, DID Commuting Zone Plot
## -------------------------------------------------------------------------- ## 

## -------------------------------------------------------------------------- ## 
## Set up: load libraries, define paths 
## -------------------------------------------------------------------------- ## 

rm(list = ls())

library(tidyverse)
library(lfe)
library(fixest)
library(modelsummary)
library(scales)
library(ggplot2)
library(fixest)
library(scales)
library(knitr)
library(stringr)

load("data/final_data.Rdata")


chips_states <- c(
  "arizona", "colorado", "florida", "georgia", "idaho", "indiana",
  "minnesota", "missouri", "north carolina", "new hampshire", "new mexico",
  "new york", "ohio", "oregon", "texas", "utah", "vermont"
)


## -------------------------------------------------------------------------- ## 
## Restructure Data for Tables and Plots
## -------------------------------------------------------------------------- ## 


df_16_24 <- merged_result %>% 
  filter(state %in% chips_states) %>%
  select(state, county, county_fips, dem_vote_2016, dem_vote_2020, dem_vote_2024, chips_county_project, chips_county, chips_mm, chips_cz) %>%
  pivot_longer(cols = c(dem_vote_2016, dem_vote_2020, dem_vote_2024),
               names_to = "year",
               values_to = "dem_percent",
               names_prefix = "dem_vote_",
               names_transform = list(year = as.integer)) %>%
  mutate(treatment = chips_county, 
         post = ifelse( year == 2024, 1, 0))


state <- merged_result %>% 
  filter(state %in% chips_states) %>%
  select(state, county, county_fips, dem_vote_2016, dem_vote_2020, dem_vote_2024, chips_county_project, chips_county, chips_mm, chips_cz) %>%
  pivot_longer(cols = c(dem_vote_2016, dem_vote_2020, dem_vote_2024),
               names_to = "year",
               values_to = "dem_percent",
               names_prefix = "dem_vote_",
               names_transform = list(year = as.integer)) %>%
  filter(year == 2020 | year == 2024) %>%
  mutate(treatment = chips_county, 
         post = ifelse( year == 2024, 1, 0))


mm <- merged_result %>% 
  filter(state %in% chips_states) %>%
  select(state, county, county_fips, dem_vote_2016, dem_vote_2020, dem_vote_2024, chips_county_project, chips_county, chips_mm, chips_cz) %>%
  pivot_longer(cols = c(dem_vote_2016, dem_vote_2020, dem_vote_2024),
               names_to = "year",
               values_to = "dem_percent",
               names_prefix = "dem_vote_",
               names_transform = list(year = as.integer)) %>%
  filter(year == 2020 | year == 2024) %>%
  mutate(treatment = chips_county, 
         post = ifelse( year == 2024, 1, 0)) %>%
  filter(chips_mm == 1)

cz <- merged_result %>% 
  filter(state %in% chips_states) %>%
  select(state, county, county_fips, dem_vote_2016, dem_vote_2020, dem_vote_2024, chips_county_project, chips_county, chips_mm, chips_cz) %>%
  pivot_longer(cols = c(dem_vote_2016, dem_vote_2020, dem_vote_2024),
               names_to = "year",
               values_to = "dem_percent",
               names_prefix = "dem_vote_",
               names_transform = list(year = as.integer)) %>%
  filter(year == 2020 | year == 2024) %>%
  mutate(treatment = chips_county, 
         post = ifelse( year == 2024, 1, 0))


## -------------------------------------------------------------------------- ## 
## CHIPS Funding Table
## -------------------------------------------------------------------------- ## 


chips_funds <- merged_result %>% group_by(county_fips) %>%
  select(state, county, chips_county_project) %>%
  filter(chips_county_project > 0) 

chips_funds$chips_county_project <- format(chips_funds$chips_county_project, scientific = FALSE)

chips_funds_clean <- chips_funds %>%
  ungroup() %>%  
  select(-county_fips)

chips_funds_clean$chips_county_project <- as.numeric(chips_funds_clean$chips_county_project)


# Capitalize state and county names if not already done
chips_funds_clean <- chips_funds_clean %>%
  mutate(state = str_to_title(state),
         county = str_to_title(county))

# Format chips_county_project as dollar currency
chips_funds_clean$chips_county_project <- dollar(chips_funds_clean$chips_county_project)

# Create LaTeX table
latex_table <- kable(
  chips_funds_clean,
  format = "latex",
  col.names = c("State", "County", "CHIPs Allocation"),
  caption = "CHIPs Allocations by County",
  booktabs = TRUE,
  align = 'l'
)


# Save latex table
file_path_table1 <- "output/chips_funding_table.txt"
writeLines(latex_table, file_path_table1)


## -------------------------------------------------------------------------- ## 
## County DID Plot
## -------------------------------------------------------------------------- ## 


trend_state <- df_16_24 %>% 
  group_by(treatment, year) %>%
  summarise(mean = mean(dem_percent)) %>%
  filter(!is.na(treatment))

state_plot <- ggplot(trend_state, aes(x = year, y = mean, group = treatment, color = factor(treatment))) +
  geom_line(size = 1.5) +
  scale_color_manual(values = c("0" = "darkblue", "1" = "darkred"),
                     labels = c("0" = "Non-CHIPS Counties", "1" = "CHIPS Counties"),
                     name = "") +
  scale_y_continuous(labels = scales::percent_format(scale = 1),
                     limits = c(20, 60),
                     breaks = seq(0, 60, by = 10)) +
  scale_x_continuous(breaks = c(2016, 2020, 2024)) +
  labs(title = "Democratic Vote Share in CHIPS and Non-CHIPS Counties",
       x = "Year",
       y = "Democratic Vote Share (%)") +
  theme_minimal() +
  theme(
    panel.grid = element_blank(),
    plot.background = element_rect(fill = "white", color = NA),
    panel.background = element_rect(fill = "white", color = NA),
    legend.position = "bottom",
    legend.background = element_rect(fill = "white", color = NA),
    plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
    axis.title = element_text(face = "bold"),
    axis.text = element_text(color = "black"),
    axis.line = element_line(color = "black")
  )


ggsave("output/state_plot.png", plot = state_plot, width = 8, height = 6, dpi = 300)

## -------------------------------------------------------------------------- ## 
## CHIPS Commuting Zone Plot
## -------------------------------------------------------------------------- ## 


trend_mm <- df_16_24 %>% 
  filter(chips_mm == 1) %>%
  group_by(chips_cz, year) %>%
  summarise(mean = mean(dem_percent)) 

mm_plot <- ggplot(trend_mm, aes(x = year, y = mean, group = chips_cz, color = factor(chips_cz))) +
  geom_line(size = 1.5) +
  scale_color_manual(values = c("0" = "darkblue", "1" = "darkred"),
                     labels = c("0" = "Non-CHIPS Commuting Zones", "1" = "CHIPS Commuting Zones"),
                     name = "") +
  scale_y_continuous(labels = scales::percent_format(scale = 1),
                     limits = c(20, 60),
                     breaks = seq(0, 60, by = 10)) +
  scale_x_continuous(breaks = c(2016, 2020, 2024)) +
  labs(title = "Democratic Vote Share in CHIPS Media Markets by Commuting Zone",
       x = "Year",
       y = "Democratic Vote Share (%)") +
  theme_minimal() +
  theme(
    panel.grid = element_blank(),
    plot.background = element_rect(fill = "white", color = NA),
    panel.background = element_rect(fill = "white", color = NA),
    legend.position = "bottom",
    legend.background = element_rect(fill = "white", color = NA),
    plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
    axis.title = element_text(face = "bold"),
    axis.text = element_text(color = "black"),
    axis.line = element_line(color = "black")
  )

ggsave("output/mm_plot.png", plot = mm_plot, width = 8, height = 6, dpi = 300)

## -------------------------------------------------------------------------- ## 
## FIGURE 1: CHIPS Funding by District
## -------------------------------------------------------------------------- ## 

rm(list = ls())

library(tidyverse)
library(ggmap)
library(ggplot2)
library(sf)
library(tigris)
library(rnaturalearth)
library(rnaturalearthhires)
library(ggpattern)

chips <- read.csv("data/chips_districts_votes.csv")


#### Map of Chips Districts ####

#create congressional district geometry
cd116 <- congressional_districts(cb = TRUE, resolution = "20m", year = 2020)

#prepare congressional geometry data to be combined with chips data
state_fips <- data.frame(
  state = c("AK", "AL", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", 
            "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", 
            "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", 
            "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", 
            "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"),
  fips = c("02", "01", "04", "05", "06", "08", "09", "10", "12", "13",
           "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
           "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
           "35", "36", "37", "38", "39", "40", "41", "42", "44", "45",
           "46", "47", "48", "49", "50", "51", "53", "54", "55", "56"))

cd116 <- cd116 %>%
  mutate(
    code = paste0(STATEFP, str_pad(CD116FP, 2, pad = "0")))

chips$code <- str_pad(chips$code, width = 4, pad = "0")

code_list <- chips$code

# Convert cd116 to sf if it isn't already
cd116_sf <- st_as_sf(cd116, wkt = "geometry")

# Filter congressional districts to only those in chips data and add chips voting data
cd116_filtered_sf <- cd116_sf %>%
  filter(code %in% code_list) %>%
  left_join(chips %>% select(code, Party, Vote, `X2020.Vote`), by = "code")

# Load U.S. state boundaries
us_states <- ne_states(country = "united states of america", returnclass = "sf")

## Create Map ##

ggplot() +
  # Add state borders
  geom_sf(data = us_states, fill = NA, color = "black", size = 0.75) +  # State borders
  # Add congressional districts with gradient fill
  geom_sf(
    data = cd116_filtered_sf,
    aes(fill = `X2020.Vote`),
    color = "black", size = 0.5
  ) +
  scale_fill_gradient2(
    low = "red",       # Color for values less than 50
    mid = "#CBC3E3",   # Neutral color around 50
    high = "blue",     # Color for values greater than 50
    midpoint = 50,     # Middle of the gradient scale
    name = "2020 Presidential Democratic Vote Share (%)"
  ) +
  coord_sf(xlim = c(-125, -65), ylim = c(25, 50), expand = FALSE) +  # Limit to the U.S. bounds
  theme_void() +  # Remove background, grids, and axis labels
  theme(panel.background = element_rect(fill = "white"),
        plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +  # Set background to white 
  labs(title = "CHIPS Funding Allocation by Congressional District and Representative Vote")

ggsave("output/figure_chips_by_district_and_vote.png", width = 20, height = 7, dpi = 300)

